home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
BYACC__
/
OUTPUT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-11-19
|
11KB
|
708 lines
#include <stdio.h>
#include "action.h"
#include "defs.h"
#include "dep.h"
#include "new.h"
#include "files.h"
#include "gram.h"
#include "state.h"
extern action **parser;
extern int final_state;
extern core **state_table;
extern shifts **shift_table;
extern reductions **reduction_table;
extern short *accessing_symbol;
extern unsigned *LA;
extern short *LAruleno;
extern short *lookaheads;
extern short *goto_map;
extern short *from_state;
extern short *to_state;
static int nvectors;
static int nentries;
static short **froms;
static short **tos;
static short *tally;
static short *width;
static short *state_count;
static short *order;
static short *base;
static short *pos;
static short *table;
static short *check;
static int lowzero;
static int high;
output()
{
free_itemsets();
free_shifts();
free_reductions();
fprintf(output_file, "\n#define YYQFINAL\t%d\n", final_state);
output_rule_data();
output_defred();
rm_defreds();
output_actions();
}
output_rule_data()
{
register int i;
register int j;
fprintf(output_file, "\nshort yylhs[] = {%6d",
symbol_value[start_symbol]);
j = 10;
for (i = 1; i < nrules; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", symbol_value[rlhs[i]]);
}
FREE(rlhs);
fprintf(output_file, "\n};\n\nshort yyrhslm1[] = { 0");
j = 10;
for (i = 1; i < nrules; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", rrhs[i + 1] - rrhs[i] - 2);
}
fprintf(output_file, "\n};\n");
FREE(rrhs);
}
output_defred()
{
register int i, j;
fprintf(output_file, "\nshort yydefred[] = {%6d", defred[0]);
j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', output_file);
if (j < 10)
j++;
else
{
putc(NEWLINE, output_file);
j = 1;
}
fprintf(output_file, "%6d", defred[i]);
}
fprintf(output_file, "\n};\n");
}
rm_defreds()
{
register int i, found, ruleno;
register action *p;
for (i = 0; i < nstates; i++)
{
ruleno = defred[i];
if (ruleno < 0)
continue;
for (p = parser[i]; p; p = p->next)
{
if (p->action_code == REDUCE && p->number == ruleno)
p->suppressed = 2;
}
found = 0;
for (p = parser[i]; p && ! found; p = p->next)
{
if (p->action_code != 0)
found = 1;
}
if (!found)
{
free_action_row(parser[i]);
parser[i] = 0;
}
}
}
output_actions()
{
nvectors = nstates + nvars;
froms = NEW2(nvectors, short *);
tos = NEW2(nvectors, short *);
tally = NEW2(nvectors, short);
width = NEW2(nvectors, short);
token_actions();
FREE(lookaheads);
FREE(LA);
FREE(LAruleno);
FREE(accessing_symbol);
goto_actions();
FREE2(goto_map,ntokens);
FREE(from_state);
FREE(to_state);
sort_actions();
pack_table();
output_base();
output_table();
output_check();
}
token_actions()
{
register int i, j, k;
register int max, min;
register short *actrow, *r, *s;
register action *p;
actrow = NEW2(ntokens, short);
for (i = 0; i < final_state; i++)
{
if (parser[i])
{
for (j = 0; j < ntokens; j++)
actrow[j] = MINSHORT;
for (p = parser[i]; p; p = p->next)
{
if (p->suppressed == 0)
{
if (p->action_code == SHIFT)
actrow[p->symbol] = p->number;
else if (p->action_code == REDUCE)
actrow[p->symbol] = - p->number;
}
}
k = 0;
min = MAXSHORT;
max = 0;
for (j = 0; j < ntokens; j++)
{
if (actrow[j] != MINSHORT)
{
k++;
if (min > symbol_value[j])
min = symbol_value[j];
if (max < symbol_value[j])
max = symbol_value[j];
}
}
if (k > 0)
{
froms[i] = r = NEW2(k, short);
tos[i] = s = NEW2(k, short);
for (j = 0; j < ntokens; j++)
{
if (actrow[j] != MINSHORT)
{
*r++ = symbol_value[j];
*s++ = actrow[j];
}
}
tally[i] = k;
width[i] = max - min + 1;
}
}
}
FREE(actrow);
}
goto_actions()
{
register int i, j, k;
state_count = NEW2(nstates, short);
k = default_goto(start_symbol);
fprintf(output_file, "\nshort yydefgoto[] = {%6d", k);
save_column(ntokens, k);
j = 10;
for (i = ntokens + 1; i < nsyms; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
k = default_goto(i);
fprintf(output_file, "%6d", k);
save_column(i, k);
}
fprintf(output_file, "\n};\n");
FREE(state_count);
}
int
default_goto(symbol)
int symbol;
{
register int i;
register int m;
register int n;
register int default_state;
register int max;
m = goto_map[symbol];
n = goto_map[symbol + 1];
if (m == n)
return (-1);
for (i = 0; i < nstates; i++)
state_count[i] = 0;
for (i = m; i < n; i++)
state_count[to_state[i]]++;
max = 0;
default_state = -1;
for (i = 0; i < nstates; i++)
{
if (state_count[i] > max)
{
max = state_count[i];
default_state = i;
}
}
return (default_state);
}
save_column(symbol, default_state)
int symbol;
int default_state;
{
register int i;
register int m;
register int n;
register short *sp;
register short *sp1;
register short *sp2;
register int count;
register int symno;
m = goto_map[symbol];
n = goto_map[symbol + 1];
count = 0;
for (i = m; i < n; i++)
{
if (to_state[i] != default_state)
count++;
}
if (count == 0)
return;
symno = symbol - ntokens + nstates;
froms[symno] = sp1 = sp = NEW2(count, short);
tos[symno] = sp2 = NEW2(count, short);
for (i = m; i < n; i++)
{
if (to_state[i] != default_state)
{
*sp1++ = from_state[i];
*sp2++ = to_state[i];
}
}
tally[symno] = count;
width[symno] = sp1[-1] - sp[0] + 1;
}
sort_actions()
{
register int i;
register int j;
register int k;
register int t;
register int w;
order = NEW2(nvectors, short);
nentries = 0;
for (i = 0; i < nvectors; i++)
{
if (tally[i] > 0)
{
t = tally[i];
w = width[i];
j = nentries - 1;
while (j >= 0 && (width[order[j]] < w))
j--;
while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
j--;
for (k = nentries - 1; k > j; k--)
order[k + 1] = order[k];
order[j + 1] = i;
nentries++;
}
}
}
pack_table()
{
register int i;
register int place;
register int state;
base = NEW2(nvectors, short);
pos = NEW2(nentries, short);
table = NEW2(MAXSHORT, short);
check = NEW2(MAXSHORT, short);
lowzero = 0;
high = 0;
for (i = 0; i < nvectors; i++)
base[i] = MINSHORT;
for (i = 0; i < MAXSHORT; i++)
check[i] = -1;
for (i = 0; i < nentries; i++)
{
state = matching_state(i);
if (state < 0)
place = pack_vector(i);
else
place = base[state];
pos[i] = place;
base[order[i]] = place;
}
for (i = 0; i < nvectors; i++)
{
FREE(froms[i]);
FREE(tos[i]);
}
FREE(froms);
FREE(tos);
FREE(pos);
}
int
matching_state(vector)
int vector;
{
register int i;
register int j;
register int k;
register int t;
register int w;
register int match;
register int prev;
i = order[vector];
if (i >= nstates)
return (-1);
t = tally[i];
w = width[i];
for (prev = vector - 1; prev >= 0; prev--)
{
j = order[prev];
if (width[j] != w || tally[j] != t)
return (-1);
match = 1;
for (k = 0; match && k < t; k++)
{
if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
match = 0;
}
if (match)
return (j);
}
return (-1);
}
int
pack_vector(vector)
int vector;
{
register int i, j, k;
register int t;
register int loc;
register int ok;
register short *from;
register short *to;
i = order[vector];
t = tally[i];
if (t == 0)
panic("pack_vector");
from = froms[i];
to = tos[i];
for (j = lowzero - from[0]; j < MAXSHORT; j++)
{
ok = 1;
for (k = 0; ok && k < t; k++)
{
loc = j + from[k];
if (loc > MAXSHORT)
fatal("maximum table size exceeded");
if (check[loc] != -1)
ok = 0;
}
for (k = 0; ok && k < vector; k++)
{
if (pos[k] == j)
ok = 0;
}
if (ok)
{
for (k = 0; k < t; k++)
{
loc = j + from[k];
table[loc] = to[k];
check[loc] = from[k];
if (loc > high)
high = loc;
}
while (check[lowzero] != -1)
lowzero++;
return (j);
}
}
panic("pack_vector");
}
output_base()
{
register int i;
register int j;
for (i = 0; i < nstates; i++)
{
j = base[i];
if (j == MINSHORT)
{
j = defred[i];
if (j >= 0)
base[i] = j;
}
else
base[i] = j - high;
}
fprintf(output_file, "\nshort yyrowbase[] = {%6d", base[0]);
j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", base[i]);
}
fprintf(output_file, "\n};\n\nshort yycolumnbase[] = {%6d", base[nstates]);
j = 10;
for (i = nstates + 1; i < nvectors; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", base[i]);
}
fprintf(output_file, "\n};\n");
FREE(base);
}
output_table()
{
register int i;
register int j;
fprintf(output_file, "\n\n#define\tYYTABLESIZE\t\t%d\n\n", high);
fprintf(output_file, "\nshort yytable[] = {%6d", table[0]);
j = 10;
for (i = 1; i <= high; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", table[i]);
}
fprintf(output_file, "\n};\n");
FREE(table);
}
output_check()
{
register int i;
register int j;
fprintf(output_file, "\nshort yycheck[] = {%6d", check[0]);
j = 10;
for (i = 1; i <= high; i++)
{
putc(',', output_file);
if (j >= 10)
{
putc('\n', output_file);
j = 1;
}
else
{
j++;
}
fprintf(output_file, "%6d", check[i]);
}
fprintf(output_file, "\n};\n");
FREE(check);
}
free_itemsets()
{
register core *cp;
register core *cp2;
FREE(state_table);
for (cp = first_state; cp;){
cp2 = cp;
cp = cp->next;
FREE(cp2);
}
}
free_shifts()
{
register shifts *sp,*sp2;
FREE(shift_table);
for (sp = first_shift; sp; ){
sp2 = sp;
sp = sp->next;
FREE(sp2);
}
}
free_reductions()
{
register reductions *rp,*rp2;
FREE(reduction_table);
for (rp = first_reduction; rp;){
rp2 = rp;
rp = rp->next;
FREE(rp2);
}
}